package com.oron3.bouncer.objects.shapes;


public class Vector extends Point {
	private float length;

	public Vector(final float x, final float y, final float z) {
		super(x, y, z);
		init();
	}
	/* (non-Javadoc)
	 * @see com.oron3.bouncer.objects.Point#set(float, float, float)
	 */
	@Override
	public void set(final float x2, final float y2, final float z2) {
		super.set(x2, y2, z2);
		init();
	}
	/* (non-Javadoc)
	 * @see com.oron3.bouncer.objects.Point#set(com.oron3.bouncer.objects.Point)
	 */
	@Override
	public void set(final Point p) {
		super.set(p);
		init();
	}
	/* (non-Javadoc)
	 * @see com.oron3.bouncer.objects.Point#move(float, float, float)
	 */
	@Override
	public void move(final float dx, final float dy, final float dz) {
		super.move(dx, dy, dz);
		init();
	}
	/**
	 * 
	 */
	private void init() {
		length = (float) Math.sqrt(dot(this));
	}

	/**
	 * @param sub
	 */
	public Vector(final Point p) {
		super(p.x, p.y, p.z);
		init();
	}

	/**
	 * 
	 */
	public Vector() {
	}
	public float dot(final Vector b) {
		return x*b.x+y*b.y+z*b.z;
	}

	/**
	 * @param t
	 * @return
	 */
	@Override
	public Vector mul(final float scalar) {
		return new Vector(scalar*x,scalar*y,scalar*z);
	}

	/**
	 * @return
	 */
	public float length() {
		return length;
	}
	/**
	 * @param p2
	 * @return
	 */
	public Vector cross(final Vector v) {
		return new Vector(y*v.z-v.y*z,z*v.x-x*v.z,x*v.y-y*v.x);
	}
	/**
	 * @return
	 */
	public Vector normalize() {
		return mul(1/length);
	}


	/**
	 * @param n
	 * @return 
	 */
	public Point reflect(final Vector n) {
		return sub(n.mul(2*dot(n)));
	}
}
